<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="内容"></el-input>
				</el-form-item>
				<el-form-item>
<!--          getshop 查询要重新回到第一页 再查询-->
					<el-button type="primary" v-on:click="querryKeyword">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
        <br>
        <el-form-item>
          <el-button type="primary" @click="onsale">上架</el-button>
          <el-button type="primary" @click="offsale">下架</el-button>
        </el-form-item>
			</el-form>
		</el-col>

<!--    @selection-change="selsChange" 批量删除选择谁就调用selsChange方法把行对象赋值给数组-->
    <!--列表-->
    <el-table :data="productOnlineAuditLog" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>id
			<el-table-column prop="product_id" label="服务id" width="80" sortable>
			</el-table-column>
			<el-table-column prop="audit_id" label="审核人id" width="80"  sortable>
			</el-table-column>
			<el-table-column prop="audit_time" label="审核时间" width="80"  sortable>
			</el-table-column>
      <el-table-column prop="note" label="备注" width="250"  sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80"  sortable>
        <template scope="scope">
          <span style="color: green" v-if="scope.row.state==1" >上架</span>
          <span style="color: red" v-else>下架</span>
        </template>
      </el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handlesave(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
<!--    :disabled="this.sels.length===0" 如果没有选择批量删除就不能点击批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="PatchRemove" :disabled="this.sels.length===0">批量删除</el-button>
<!--      分页栏-->
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize"  :total="totals" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增和修改界面-->
<!--    :visible.sync="saveFormVisible" 模态框-->
<!--    :model="saveForm" 绑定数据-->
<!--    :rules="saveFormRules" - 表单的验证规则-->
<!--    prop="username" 绑定行类表单验证-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false" width="80%">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="服务名称" prop="name" width="200">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="成本价格"  width="200">
          <el-input v-model="saveForm.costprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价" prop="url" width="200">
          <el-input v-model="saveForm.saleprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="销售数量"  width="200">
          <el-input v-model="saveForm.salecount" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片"  width="200">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
          <el-form-item label="简介" prop="intro">
            <!-- v-if="saveForm.productOnlineAuditLogDetail" 修改v-model绑定对象报错问题，v-if对需要显示的数据判断下 -->
            <quill-editor  v-model="saveForm.productOnlineAuditLogDetail.intro" v-if="saveForm.productOnlineAuditLogDetail" :options="quillOption">
            </quill-editor>
          </el-form-item>
          <el-form-item label="预约须知" prop="orderNotice">
            <quill-editor  v-model="saveForm.productOnlineAuditLogDetail.orderNotice" v-if="saveForm.productOnlineAuditLogDetail" :options="quillOption">
            </quill-editor>
          </el-form-item>
        </el-form-item>
        <el-form-item label="状态"  width="200">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">上架</el-radio>
            <el-radio class="radio" :label="0">下架</el-radio>
          </el-radio-group>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
//引入Vue的ueditor的资源
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from '../../common/js/quill-config';
	export default {
    components: {
      quillEditor
    },
		data() {
			return {
        quillOption: quillConfig,
			  //列表绑定数据
        productOnlineAuditLog: [],
        listLoading: false,
        currentPage:1,
        pageSize:5,
        totals:50,
        // 高级查询列表框初始为空字符串
        keyword:"",
        sels: [],//列表选中列


				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
        saveFormRules: {
          name: [
						{ required: true, message: '请输入服务名称', trigger: 'blur' }
					],
				},
				//编辑界面数据
				saveForm: {
          id:null,
          name:'',
          costprice:null,
          saleprice:null,
          resources:'',
          salecount:null,
          state:0,
          productOnlineAuditLogDetail:{  //与后端实体类一致
            productOnlineAuditLogId:null,
            intro: '',
            orderNotice: ''
          }
				},
        title:'',
        fileList:[],
			}
		},
		methods: {
		  //页面变化事件
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getproductOnlineAuditLog();
			},
			//获取用户列表
      getproductOnlineAuditLog() {
				let para = {
          currentPage: this.currentPage,
					pageSize: this.pageSize,
          keyword:this.keyword
				};
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/productOnlineAuditLog",para).then((res) => {
					this.totals = res.data.totals;
					this.productOnlineAuditLog = res.data.data;
					this.listLoading = false;
				});
			},
      //高级查询查询点击事件
      querryKeyword(){
			  //将页面置为第一页
        this.currentPage=1
        //调用查询高级查询输入框的内容,和分页数据
        this.getproductOnlineAuditLog();
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/productOnlineAuditLog/"+row.id).then((res) => {
						this.listLoading = false;
						//删除3个点,是否为最后一页,是否为最后一条数据,不是第一页
            let last=Math.ceil(this.totals/this.pageSize)
						if(this.currentPage!=1 && last==this.currentPage && (this.totals-1)%this.pageSize==0){
						    this.currentPage=this.currentPage-1
                this.getproductOnlineAuditLog();
            }
					if (res.data.success){
					  this.$message.success("删除成功")
          }else {
					  this.$message.error("删除失败500")
          }
						this.getproductOnlineAuditLog();
					});
				}).catch(() => {
          this.$message.error("删除失败400")
				});
			},
			//显示修改界面
			handlesave: function (index, row) {
        //修改  模态框回显菜单名字
        this.title="修改";
				this.saveFormVisible = true;//打开模态框
				this.saveForm = Object.assign({}, row);//.assign把对象拷贝一份给新数字,把对象赋值给新数组然后再回显
        this.fileList=[]
        if(this.saveForm.resources){
          var arr=this.saveForm.resources.split(",")
          for (var i=0;i<arr.length;i++){
           var item= arr[i];
            this.fileList.push({"url":"http://123.207.27.208" + item})
          }
        }
        this.$refs.saveForm.clearValidate();
			},
			//显示新增界面
			handleAdd: function () {
        //清空fileList的数据
        this.fileList = [];
        this.title="新增";
				this.saveFormVisible = true;
				//点击新增重置模态框
				this.saveForm = {
          id:null,
          name:'',
          costprice:null,
          saleprice:null,
          type_id:null,
          resources:'',
          state:0,
          shop_id:null,
          productOnlineAuditLogDetail:{  //与后端实体类一致
            intro: '',
            adoptNotice: ''
          }
				};
        // this.$refs.saveForm.clearValidate();
			},
			//新增和修改
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {//表单验证
					if (valid) {//验证成功true
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  this.saveFormVisible=false;//确认提交关闭模态框
							this.saveLoading = true;//打开盲等框
							//NProgress.start();
							let para = Object.assign({}, this.saveForm);//获取表单对象
              let arr = para.type_id;
              if(arr && arr.length==0){
                para.type_id = null;
              }
              if(arr && arr.length>0){ //[1,13]
                para.type_id = arr[arr.length-1];
              }
							this.$http.put("/productOnlineAuditLog",para).then(result=>{
                if(result.data.success){
                  this.$message.success("操作成功")
                  this.getproductOnlineAuditLog();//刷新界面
                }
              }).catch(result=>{
                this.$message.error("操作失败500")
              })
              this.saveLoading = false;//成功关闭盲等框
						}).catch(()=>{
              this.$message.error("操作失败400")
            });
					}
				});
			},
      //批量删除点击谁就把那一行的对象赋值给sels
			selsChange: function (sels) {
				this.sels = sels;
				console.log(this.sels)
			},
			//批量删除
			PatchRemove: function () {
			  //只拿id   将每一行id装成数组
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.patch("/productOnlineAuditLog",ids).then((res) => {
						this.listLoading = false;
						if (res.data.success){
						  this.$message.success("删除成功")
            }else {
						  this.$message.success("删除失败500")
            }
						this.currentPage=1;
						this.getproductOnlineAuditLog();
					});
				}).catch(() => {
				});
			},
      handleSuccess(response, file, fileList){
        console.log(response)
        console.log(file);

        var src=response.resultObj;// /group1/M00/00/EF/CgAIC2LdCniANnu8AADYXI6DDUY559.jpg
        //1.对resources的处理 - 添加到数据库
        if(this.saveForm.resources){
          this.saveForm.resources=this.saveForm.resources+","+src
        }
        else {
          this.saveForm.resources=response.resultObj
        }
        this.fileList=[];
        if(this.saveForm.resources){
         var arr= this.saveForm.resources.split(",");
          for(var i=0;i<arr.length;i++){
            var item =fileList=arr[i];
            //"url":"blob:http://localhost:8081/2496bb6b-25a8-41e8-b039-02ea5e72e37f"
            this.fileList.push({"url":"http://123.207.27.208" + item})
          }
        }

      },
      handleRemove(file, fileList){
          console.log(file);
        // "http://123.207.27.208/group1/M00/00/F4/CgAIC2LdKWSAQDz2AADuOSpWHy8556.jpg"
          var path=file.url.substring(file.url.indexOf("/group1"));
          this.$http.delete("/fastDfs?path="+path).then(result=>{
            this.$message.success("删除成功")
          }).catch(result=>{
            this.$message.error("删除成功")
          })
        //对resources处理
        if(this.saveForm.resources){
         var arr=this.saveForm.resources.split(",");
          for (var i=0;i<arr.length;i++){
            if(arr[i]==path){
              arr.splice(i,1);
            }
          }
          this.saveForm.resources=arr.join(",");
        }
        //对fileList处理
        this.fileList=[]
        if(this.saveForm.resources){
           var arr=this.saveForm.resources.split(",");
           for (var i=0;i<arr.length;i++){
            var item= arr[i];
            this.fileList.push({"url":"http://123.207.27.208" +item})
           }
        }

      },
      //上架
      onsale(){
        var ids = this.sels.map(item => item.id);
        //获取选中的行
        if(!this.sels || this.sels.length  <1){
          this.$message({ message: '老铁，你不选中数据，臣妾上架不了啊....',type: 'error'});
          return;
        }
        var sta = this.sels.map(item => item.state);
        if(sta.indexOf(1)!=-1){
          this.$message({ message: '老铁，请勿重复上架',type: 'error'});
          return;
        }
        this.$confirm('确认上架选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.post('/productOnlineAuditLog/onsale',ids).then((res) => {
            this.listLoading = false;
            if(res.data.success){
              this.$message.success(res.data.msg)
            }else{
              this.$message.error(res.data.msg)
            }
            this.getproductOnlineAuditLog();
          }).catch(res => {


          })
        }).catch(() => {
        });
      },

//下架
      offsale(){
        var ids = this.sels.map(item => item.id);
        //获取选中的行
        if(!this.sels || this.sels.length  <1){
          this.$message({ message: '老铁，你不选中数据，臣妾下架不了啊....',type: 'error'});
          return;
        }
        var sta = this.sels.map(item => item.state);
        if(sta.indexOf(0)!=-1){
          this.$message({ message: '老铁，请勿重复下架',type: 'error'});
          return;
        }
        this.$confirm('确认下架选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.post('/productOnlineAuditLog/offsale',ids).then((res) => {
            this.listLoading = false;

            if(res.data.success){
              this.$message({
                message: '下架成功',
                type: 'success'
              });
            }else{
              this.$message({
                message: res.data.message,
                type: 'error'
              });
            }
            this.getproductOnlineAuditLog();
          }).catch(res => {
            this.$message({
              message: "系统错误，请稍后重试!!!",
              type: 'error'
            });
          })
        }).catch(() => {
        });
      },
		},
    //钩子方法页面加载完成就调用查询所有方法
		mounted() {
			this.getproductOnlineAuditLog();
		}

	}

</script>

<style scoped>

</style>